#include "head.h"
// 检测执行语句返回是否为空
// 参数1：mysql 指针
int sql_watch(MYSQL *mysqls){
    // 获得查询结果
    MYSQL_RES *res = mysql_store_result(mysqls);
    // 获得查询结果的列
    unsigned int files = mysql_num_fields(res);
    if(files == 0){
        return 1;
    }
    if(mysql_num_rows(res) == 0){
        mysql_free_result(res);
        return 1;
    }
    // 释放获得结果
    mysql_free_result(res);
    return 0;

}


// 服务端mkdir命令实现
// 参数1，文件描述符
// 参数2，协议结构体
int create_dir(int fd, transport_protocol_t*msg){
    int i = 0;
    int ret = 0;
    MYSQL_RES *result;
    MYSQL_ROW row;
    while(1){ // 循环判断对方是否跨目录创建目录
        if(msg->buf[i] == '\0'){
            break;
        }                                         
        if(msg->buf[i] == '/'){
            ret = -1;
            send(fd, &ret, sizeof(int), 0);
            return 0;
        }
        i++;
    }
    char buffer[1024] = {0};
    // 判断用户是否处于根目录
    char buf2[500] = { 0 };
    strcpy(buf2, msg->curr_path);
    if(strcmp(msg->curr_path, "/") == 0){
        bzero(msg->curr_path,sizeof(msg->curr_path));
    }
    sprintf(buffer, "select *from bd_file where user_id = %s"
            " and filename = '%s' and url = '%s/%s' and delete_flag=0",
            msg->user_id, msg->buf, msg->curr_path, msg->buf);
    //测试打印
    mysql_query(&msg->my_sql, buffer);
    char str[40] = {0};
    if(mysql_error(&msg->my_sql) != NULL){
        sprintf(str,"mkdir mysql_error:%s", mysql_error(&msg->my_sql));
        LOG_WRITE( 3, str);
    }
    ret = sql_watch(&msg->my_sql);
    if(ret == 0){
        ret = -1;
        send(fd, &ret, sizeof(int), 0);
        return 0;
    }
    // 检测该文件夹是否曾经被创建过
    // 如果曾经被创建过则把删除位改回0
    bzero(buffer, sizeof(buffer));
    sprintf(buffer, "select *from bd_file where user_id = %s"
        " and filename = '%s' and url = '%s/%s' and delete_flag= 1",
        msg->user_id, msg->buf, msg->curr_path, msg->buf);
    mysql_query(&msg->my_sql, buffer);
    bzero(str,sizeof(str));
    if(mysql_error(&msg->my_sql) != NULL){
        sprintf(str,"mkdir mysql_error:%s", mysql_error(&msg->my_sql));
        LOG_WRITE( 3, str);
    }

    ret = sql_watch(&msg->my_sql);
    if(ret == 0){
        ret = 1;
        bzero(buffer,sizeof(buffer));
        sprintf(buffer,"update bd_file set delete_flag = 0 where "
                "user_id = %s and filename = '%s' and url = '%s/%s'",
                msg->user_id, msg->buf, msg->curr_path, msg->buf);
        mysql_query(&msg->my_sql, buffer);
        send(fd, &ret, sizeof(int), 0);
        return 0;
    }

    // 运行到此处说明当前目录并没有这个目录，可以创建目录            
    // 清空数组再次利用
    bzero(buffer, sizeof(buffer));
    // 找到父目录的id
    sprintf(buffer, "select file_id from bd_file where user_id = %s"
            " and url = '%s'", msg->user_id, buf2);
    mysql_query(&msg->my_sql, buffer);
    result = mysql_store_result(&msg->my_sql);
    unsigned int files = mysql_num_fields(result);
    
    char buf4[10] = {0};
    while((row = mysql_fetch_row(result)) != NULL){
        for(unsigned int i = 0; i < files; i++){
            sprintf(buf4, "%s",row[i]);
        }
    }

    bzero(str,sizeof(str));
    sprintf(str, "父目录buf4:%s", buf4);
    info_write(2, str);
    //LOG_WRITE( 2, str);
    // 插入数据
    bzero(buffer, sizeof(buffer));
    sprintf(buffer, "insert into bd_file (file_id, filename,"
            " user_id, parent_id, url, file_type, file_hash,"
            "delete_flag, upload_flag)values(NULL, '%s',%s, %s, '%s/%s', "
            " 1, NULL, 0, NULL)",
            msg->buf, msg->user_id, buf4, msg->curr_path,
            msg->buf);
    // 执行插入数据语句
    mysql_query(&msg->my_sql, buffer);

    mysql_free_result(result);

    ret = 0;
    send(fd, &ret, sizeof(int), 0);
    return 0;
}

